package com.deer.wms.inventory.web;

import com.deer.wms.inventory.model.Inventory.InventoryTransact;
import com.deer.wms.inventory.model.Inventory.InventoryTransactCriteria;
import com.deer.wms.inventory.model.Inventory.InventoryTransactDto;
import com.deer.wms.inventory.service.InventoryTransactService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;

import java.util.List;

import static com.deer.wms.project.root.util.PageUtils.getStart;

/**
* Created by 郭靖勋 on 2019/12/18.
*/
@Api(description = "库存事务api接口")
@RestController
@RequestMapping("/inventory/transacts")
public class InventoryTransactController {

    @Autowired
    private InventoryTransactService inventoryTransactService;

    @PostMapping("/insert")
    public Result add(@RequestBody InventoryTransact inventoryTransact) {
        inventoryTransactService.save(inventoryTransact);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/delete")
    public Result delete(Integer id) {
        inventoryTransactService.removeById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody InventoryTransact inventoryTransact) {
        inventoryTransactService.updateById(inventoryTransact);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/detail")
    public Result detail(@PathVariable Integer id) {
        InventoryTransact inventoryTransact = inventoryTransactService.getById(id);
        return ResultGenerator.genSuccessResult(inventoryTransact);
    }

    @GetMapping("/list")
    public Result list(InventoryTransactCriteria criteria) {
//        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        Integer pageNum = criteria.getPageNum();
        criteria.setPageNum(getStart(criteria));
        List<InventoryTransactDto> list = inventoryTransactService.findList(criteria);
        PageInfo pageInfo = new PageInfo(list);
        pageInfo.setPageNum(pageNum);
        pageInfo.setTotal(inventoryTransactService.findCount(criteria));
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    //调整库存
    @PostMapping("/addTransact")
    public Result insert(@RequestBody InventoryTransact inventoryTransact) {
        inventoryTransactService.run(inventoryTransact);
        return ResultGenerator.genSuccessResult();
    }

}
